//
//  ScrollViewController.swift
//  swift各类控件集锦
//
//  Created by iosdev1 on 2017/4/21.
//  Copyright © 2017年 zyy. All rights reserved.
//

import UIKit

class ScrollViewController: ZyBaseViewController, UIScrollViewDelegate{

    var lastContentOffset:CGFloat = 0.0
    var currentContentOffset:CGFloat = 0.0
    
    var timer:Timer? = nil
    var i:CGFloat? = 1.0 //记录
    
    var mainView:UIScrollView! = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        mainView = UIScrollView.init(frame: CGRect(x:0, y:0, width:WIDTH_SCREEN, height:HEIGHT_SCREEN))
        self.view.addSubview(mainView)
        
        let arrImage = ["Guide_Five","Guide_One","Guide_Two","Guide_Three","Guide_Four","Guide_Five","Guide_One"]
        for i in 0 ..< arrImage.count {
            let ivImage:UIImageView = UIImageView.init(image: UIImage.init(named: arrImage[i]))
            mainView.addSubview(ivImage);
            ivImage.frame = CGRect(x:WIDTH_SCREEN * CGFloat(i), y:0, width:WIDTH_SCREEN, height:HEIGHT_SCREEN)
        }
        
        self.view.backgroundColor = UIColor.red
        
        mainView.backgroundColor = UIColor.yellow
            // 是否支持滑动最顶端
        mainView.scrollsToTop = true
         // 是否反弹
        mainView.bounces = true
            // 是否分页
        mainView.isPagingEnabled = true
          // 是否滚动    
        mainView.isScrollEnabled = true
        
        // 隐藏垂直滚动条
        mainView.showsVerticalScrollIndicator = false
        // 隐藏水平滚动条
        mainView.showsHorizontalScrollIndicator = true
        
        // 设置indicator风格
        mainView.indicatorStyle = UIScrollViewIndicatorStyle.black
        
// 设置内容的边缘    UIEdgeInsetsMake(<#T##top: CGFloat##CGFloat#>, <#T##left: CGFloat##CGFloat#>, <#T##bottom: CGFloat##CGFloat#>, <#T##right: CGFloat##CGFloat#>)
        mainView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)// 增加额外的滚动区域
        //        Indicators边缘
        mainView.scrollIndicatorInsets = UIEdgeInsetsMake(100, 50, 60, 10)
        // 提示用户,Indicators flash
        mainView.flashScrollIndicators()
        // 是否同时运动,lock
        mainView.isDirectionalLockEnabled = false
//        // 用来记录scrollview滚动的位置
        mainView.contentOffset = CGPoint(x:20, y:40)
//        设置初始位置
        mainView.setContentOffset(CGPoint(x:WIDTH_SCREEN, y:0), animated: true)
        
        mainView.layer.masksToBounds = true
        
        mainView.isUserInteractionEnabled = true
        
        // 设置UIScrollView的滚动范围（内容大小）
        mainView.contentSize = CGSize(width:WIDTH_SCREEN * CGFloat(arrImage.count), height:0)
        
        mainView.delegate = self
        
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerClick), userInfo: "timer", repeats: true)
    }

    
//    MARK: 实现代理方法
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print("已经滑动")
    }
    
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        print("视图已经放大或缩小")
    }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        lastContentOffset = scrollView.contentOffset.x
        
        if scrollView.contentOffset == CGPoint(x:WIDTH_SCREEN * 5, y:0) {
            scrollView.setContentOffset(CGPoint(x:0, y:0), animated: false)
        }else if scrollView.contentOffset == CGPoint(x:WIDTH_SCREEN * 0, y:0) {
            scrollView.setContentOffset(CGPoint(x:WIDTH_SCREEN * 5, y:0), animated: false)
        }
    }
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        print("scrollViewWillEndDragging")
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        print("结束拖动")
    }
    
    func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
//        print("开始减速")
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        print("减速停止")
        currentContentOffset = scrollView.contentOffset.x
        let p:CGPoint = scrollView.contentOffset
        let x = p.x / WIDTH_SCREEN
//        左划，向左滚动
        if lastContentOffset < currentContentOffset {
            i = x
        }
//        右划，向右滚动
        else {
        i = x - 1.0
        }
    }
    
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        print("结束动画")
    }
    
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        print("返回一个放大或者缩小的视图")
        let view = UIView.init()
        let lblTitle = UILabel.init(frame: CGRect(x:5, y:5, width:100, height:50))
        view.addSubview(lblTitle)
        lblTitle.text = "放大或者缩小的视图"
        return view
    }
    
    func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
        print("开始放大或者缩小")
    }
    
    func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
        print("缩放结束时")
    }

    func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
//        print("是否支持滑动至顶部")
        return true
    }
    
    func scrollViewDidScrollToTop(_ scrollView: UIScrollView) {
//        print("滑动到顶部时调用该方法")
    }
    
    func timerClick() {
        i = i! + 1.0
        if self.mainView.contentOffset == CGPoint(x:WIDTH_SCREEN * 5, y:0) {
            self.mainView.setContentOffset(CGPoint(x:0, y:0), animated: false)
            i = 1.0
        }
        self.mainView.setContentOffset(CGPoint(x:WIDTH_SCREEN * i!, y:0), animated: false)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
